package com.food.platformmanagement.application;

import com.food.platformmanagement.domain.User;
import com.food.platformmanagement.adapter.inbound.UserDTO;
import com.food.platformmanagement.domain.UserName;
import com.food.platformmanagement.domain.UserPassword;
import com.food.platformmanagement.port.inbound.MemberUseCase;
import com.food.platformmanagement.port.outbound.UserRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 24270
 */
@Service
@Transactional
@AllArgsConstructor
public class UserApplicationService implements MemberUseCase {
    private final UserRepository repository;

    /**
     * 添加用户
     * @param name
     * @param password
     * @return id
     */
    @Override
    public String addPlatformUser(String name, String password) {
        UserName userName = new UserName(name);
        UserPassword userPassword = new UserPassword(password);
        User user = new User(userName, userPassword);
        repository.addPlatformUser(user);
        return user.id();
    }

    /**
     * 删除用户
     * @param id
     * @return 是否成功
     */
    @Override
    public boolean deletePlatformUser(String id) {
        try {
            repository.deletePlatformUser(id);
            return true;
        }catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }

    /**
     * 查询所有用户
     * @return
     */
    @Override
    public List<UserDTO> getAllPlatformUser() {
       return repository.getAllPlatformUser().stream()
               .map(User::makeUserDTO)
               .collect(Collectors.toList());
    }

    /**
     * 查询指定用户
     * @param id
     * @return
     */
    @Override
    public UserDTO getThePlatformUser(String id) {
        return repository.getThePlatformUser(id).makeUserDTO();
    }

    @Override
    public boolean changeUserName(String id, String name) {
        User user = repository.getThePlatformUser(id);
        if(user != null) {
            user.changeName(name);
            repository.chagePlatformUser(user);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean changeUserPassword(String id, String password) {
        User user = repository.getThePlatformUser(id);
        UserPassword userPassword = new UserPassword(password);
        if(user != null) {
            user.changePassword(userPassword.value());
            repository.chagePlatformUser(user);
            return true;
        }else {
            return false;
        }
    }


    /**
     * 添加审核员
     * @param name
     * @param password
     * @return 审核员id
     */
    @Override
    public String addAuditor(String name, String password) {
        UserName userName = new UserName(name);
        UserPassword userPassword = new UserPassword(password);
        User user = new User(userName, userPassword);
        user.changeStatus("AUDITOR");
        repository.addPlatformUser(user);
        return user.id();
    }

    /**
     * 删除审核员
     * @param id
     * @return boolean
     */
    @Override
    public boolean deleteAuditor(String id) {
        try {
            repository.deleteAuditor(id);
            return true;
        }catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }

    /**
     * 查询所有审核员
     * @return 所有审核员List
     */
    @Override
    public List<User> getAllAuditor() {
        return repository.getAllAuditor();
    }

    /**
     * 查询指定审核员
     * @param id
     * @return 指定审核员
     */
    @Override
    public User getTheAuditor(String id) {
        return repository.getTheAuditor(id);
    }
}
